<template>
  <div class="album">
    <!-- 搜索区域 -->
    <el-form class="album_nav">
      <el-button v-if="button1" type="primary" size="medium" @click="creat()">添加一条</el-button>
    </el-form>
    <el-table
      :data="albumListDtoList"
      style="width: 100%"
      :border="true"
      :highlight-current-row="true"
    >
      <el-table-column label="序号" align="center" width="100">
        <template slot-scope="scope">
          <span>{{ scope.row.album_id }}</span>
        </template>
      </el-table-column>
      <el-table-column label="相片" align="center">
        <template slot-scope="scope">
          <div slot="reference" class="name-wrapper">
            <img style="width:100px;height:100px" :src="scope.row.album_picture" alt="图片加载失败">
          </div>
        </template>
      </el-table-column>
      <el-table-column label="标题" align="center" width="250">
        <template slot-scope="scope">
          <div slot="reference" class="name-wrapper">
            <el-tag size="medium">{{ scope.row.album_name }}</el-tag>
          </div>
        </template>
      </el-table-column>

      <el-table-column label="上传时间" align="center">
        <template slot-scope="scope">
          <div slot="reference" class="name-wrapper">
            <span>{{ scope.row.album_time }}</span>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button v-if="button2" size="medium" type="success" @click="updata(scope.row)">修改</el-button>
          <el-button v-if="button3" size="medium" type="danger" @click="deleteData(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 修改一条 -->
    <el-dialog :visible.sync="dialogFormVisible">
      <el-form
        :rules="rules"
        :model="data"
        label-position="left"
        label-width="70px"
        style="width: 400px; margin-left:50px;height:300px;"
      >
        <el-form-item v-if="ddd" label="序号:">
          <span>{{ temp.album_id }}</span>
        </el-form-item>
        <el-form-item label="标题:">
          <el-input v-model="temp.album_name" />
        </el-form-item>
        <el-form-item label="图片:">
          <el-upload
            class="avatar-uploader"
            action="https://jsonplaceholder.typicode.com/posts/"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
          >
            <img v-if="temp.album_picture" :src="temp.album_picture" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon" />
          </el-upload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button v-if="aaa" type="primary" @click="upData">更改</el-button>
        <el-button v-if="bbb" type="primary" @click="creatData">新增</el-button>
      </div>
    </el-dialog>
    <!-- 确认删除 -->
    <el-dialog :visible.sync="deleteDataFrom" width="30%">
      <el-form
        :rules="rules"
        :model="temp"
        label-position="left"
        label-width="70px"
        style="width: 400px; margin-left:50px;"
      >
        <span>确定删除这一条吗？</span>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="deleteDataFrom = false">取消</el-button>
        <el-button type="primary" @click="deleteDatas">确定删除</el-button>
      </div>
    </el-dialog>
    <!-- 分页器 -->
    <el-col :span="24" class="toolbar" style="text-align: center;">
      <el-pagination
        background
        :current-page="currentPage"
        :page-count="pageNum"
        :page-size="pagesize"
        style="margin-top:30px;margin-bottom:30px"
        layout="total, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-col>
  </div>
</template>
<script>
import request from '@/utils/request'
import { Message, popconfirm } from 'element-ui'
import { getToken, getAccount, getButtonpermsStr } from '@/utils/auth'
export default {
  data() {
    return {
      albumListDtoList: [],
      currentPage: 1, // 初始页
      pagesize: 10, // 当前页面内的列表行数
      pageNum: 1, // 总数页数
      total: 0,
      dialogFormVisible: false,
      deleteDataFrom: false,
      rules: {},
      temp: {
        album_id: '',
        album_picture: '',
        album_name: '',
        album_time: new Date()
      },
      imageUrl: '',
      aaa: false, // 修改
      bbb: true, // 新增
      ccc: false, // 旧图片
      ddd: false, // 序号
      button1: false,
      button2: false,
      button3: false,
      buttonpermsStr: getButtonpermsStr(),
      data: {}
    }
  },
  mounted() {
    if (this.buttonpermsStr !== undefined) {
      if (this.buttonpermsStr.indexOf(168) > -1) {
        this.button1 = true
      }
      if (this.buttonpermsStr.indexOf(169) > -1) {
        this.button2 = true
      }
      if (this.buttonpermsStr.indexOf(170) > -1) {
        this.button3 = true
      }
    }
    this.getList()
  },
  methods: {
    // 删除
    deleteData(row) {
      this.deleteDataFrom = true
      this.temp = {
        album_id: row.album_id
      }
    },
    // 确认删除
    deleteDatas() {
      return request({
        url: '/v3/album/delete',
        method: 'post',
        headers: {
          account_type: getAccount()
        },
        data: this.temp
      }).then(res => {
        this.deleteDataFrom = false
        this.$router.go(0) // 刷新页面
      })
    },
    // 创建
    creat() {
      this.dialogFormVisible = true
      this.aaa = false
      this.bbb = true
      this.ccc = false
      this.ddd = false
      this.temp = {
        album_picture: '',
        album_name: '',
        album_time: new Date()
      }
    },
    // 修改
    updata(row) {
      this.dialogFormVisible = true
      this.aaa = true
      this.bbb = false
      this.ccc = true
      this.ddd = true
      this.temp = {
        album_id: row.album_id,
        album_picture: row.album_picture,
        album_name: row.album_name,
        album_time: new Date()
      }
    },
    // 确认新增
    creatData() {
      return request({
        url: '/v3/album/inset',
        method: 'post',
        headers: {
          account_type: getAccount()
        },
        data: this.temp
      }).then(res => {
        this.dialogFormVisible = false
        this.$router.go(0) // 刷新页面
      })
    },
    // 确认修改
    upData() {
      return request({
        url: '/v3/album/update',
        method: 'post',
        headers: {
          account_type: getAccount()
        },
        data: this.temp
      }).then(res => {
        this.dialogFormVisible = false
        this.$router.go(0) // 刷新页面
      })
    },
    // 当前页
    handleSizeChange: function(pagesize) {
      this.pagesize = pagesize
      this.getList()
    },
    // 换页：更新列表数据
    handleCurrentChange: function(currentPage) {
      this.currentPage = currentPage
      this.getList()
    },

    handleAvatarSuccess(res, file) {
      this.temp.album_picture = URL.createObjectURL(file.raw)
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg'
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isJPG && isLt2M
    },

    // 获取所有列表
    getList() {
      return request({
        url: '/v3/album/list',
        method: 'post',
        headers: {
          account_type: getAccount()
        },
        data: {
          album_id: 0,
          current_page: 0,
          page_size: 0
        }
      }).then(res => {
        res.albumListDtoList.map((value, index) => {
          var d = new Date(value.album_time)
          value.album_time =
            d.getFullYear() +
            '-' +
            (d.getMonth() + 1) +
            '-' +
            d.getDate() +
            ' ' +
            d.getHours() +
            ':' +
            d.getMinutes() +
            ':' +
            d.getSeconds()
        })
        this.albumListDtoList = res.albumListDtoList
        this.currentPage = res.current_page
        this.pageNum = res.pages
        this.total = res.total_record
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.album {
  margin-top: 55px;
  overflow: hidden;
  .album_nav {
    margin: 10px;
  }
}
.avatar-uploader {
  border: 1px solid #409eff;
  width: 180px;
  height: 180px;
}
.avatar-uploader .el-upload {
  border: 1px dashed black;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
